#DIFF_FLAGS = -y --left-column -W 160
DIFF_FLAGS = -y --suppress-common-lines -W 160

# target default:
#   Default targets are dangerous
default:
	@echo "Specify a target."""

# target test:
#   Run all unit tests
test:
	@echo "##############################################################"
	@echo "Testing module parser"
	meta parser_test.py

# target betatest:
#   Run all unit tests
betatest:
	@echo "##############################################################"
	@echo "Testing module parser"
	meta -V beta parser_test.py
	cd .meta/oopl/python; make test

# target py:
#   Test the python.py module.
py:
	python python_test.py

# target tr:
#   Recreate tmp/parser.meta from parser.py and parser_test.py
tr:
	mkdir -p tmp
	cd tmp; \
	rm -f parser.meta; \
	metac -b python -R ../parser.py; \
	metac -b python -c parser.meta; \
	diff $(DIFF_FLAGS) ../parser.py .meta/oopl/python/parser.py > parser.diff  || ignore_exit=1; \
	diff ../parser.py .meta/oopl/python/parser.py > parser.diff.patch || ignore_exit=1; \
	diff $(DIFF_FLAGS) ../parser_test.py .meta/oopl/python/parser_test.py > parser_test.diff  || ignore_exit=1; \
	diff ../parser_test.py .meta/oopl/python/parser_test.py > parser_test.diff.patch || ignore_exit=1

# target rr:
#   Compile the parser.meta file created with 'reverse' from parser.py
rr:
	if [ ! -f "tmp/parser.meta" ] ; then make reverse; fi
	cd tmp; \
	metac -b python -c parser.meta

# target rrd:
#   Compile the parser.meta file created with 'reverse' from parser.py,
#   with debugging enabled.
rrd:
	if [ ! -f "tmp/parser.meta" ] ; then make reverse; fi
	cd tmp; \
	metac -A 10 -b python -c parser.meta


# target person:
#   Compile a test module.
person:
	@echo "##############################################################"
	@echo "Compiling testdata/person.meta"
	metac -b py -c testdata/person.meta
	@echo "##############################################################"
	@echo "Running person code"
	cd testdata; metapy person
	@echo "##############################################################"
	@echo "Running person unit tests"
	cd testdata; metapy -t person
	@echo "##############################################################"

# target type:
#   Compile type.meta.
type:
	metac -b py -c testdata/type.meta	

# target diff-%:
#   See differences in a given module.
diff-%:
	if [ -e "$*.py" ] ; then \
		svn diff --diff-cmd diff -x '-y -W 160 --suppress-common-lines' $*.py; \
	else \
		svn diff --diff-cmd diff -x '-y -W 160 --suppress-common-lines' $*; \
	fi

# target metacomp:
#   Compile the Meta source for Meta.
metacomp: parser2.meta parser.py
	metac -b python -c parser2.meta

# target metarun:
#   Run the parser2.meta code
metarun: parser2.meta parser.py
	metapy parser2.Type

# target metatest:
#   Run the test harness for parser2.meta
metatest: parser2.meta parser.py
	metapy -t parser2.Type

# target metaall:
#   Compile, test and run parser2.meta
metaall: metacomp metatest metarun

# target dcompile:
#   Compile code withd parser debugging turned on.
dcompile: parser2.meta parser.py
	metac -A 10 -b python -c parser2.meta

# target queens-%:
#   Compile queens into specified language and run.
queens-%:
	cd ex; metac -b $* -c queens.py; \
	if [ "$*" = "python" ] ; then \
		metajava queens; \
	elif [ "$*" = "java" ] ; then \
		metapy queens; \
	fi

# target emacs:
#  Make the major mode code
emacs:
	metac -E

# target bootstrap:
#   Rebuild the bootstrapped files.
bootstrap:
	gtest parser Compiler.generateBootstraps

# target frozen:
#   Test a compilation environment start to finish
frozen:
	cd testdata; make frozen

# target check:
#   Perform a variety of actions to verify that the compiler is working as
#   intended.
check: frozen metacomp metatest test

# target kernel-beta:
#   Ensure that metac and the meta* runtimes have access to the beta
#   kernel code in $METAROOT/src/kernel, rather than using the frozen 
#   kernel code in $METAROOT/lib/current/meta
kernel-beta:
	@cd ../../lib/beta; \
	rm -f metaboot; \
	ln -s ../../src/kernel metaboot

# target kernel-stable:
#   Ensure that metac and the meta* runtimes are using the stable
#   (non beta) kernel code in $METAROOT/lib/current/meta instead
#   of that in $METAROOT/src/kernel.
kernel-stable:
	@rm -f ../../lib/beta/metaboot

# target metalib-beta:
#   Ensure that metac and the meta* runtimes have access to the beta
#   meta library code in $METAROOT/src/kernel/.meta/oopl, rather than
#   using the frozen lib code in $METAROOT/lib/current.
metalib-beta:
	@cd ../../lib/beta; \
	rm -f lib; \
	ln -s ../../src/kernel/.meta/oopl lib

# target metalib-stable:
#   Ensure that metac and the meta* runtimes are using the stable
#   (non beta) kernel code in $METAROOT/lib/current/meta instead
#   of that in $METAROOT/src/kernel.
metalib-stable:
	@rm -f ../../lib/beta/lib

# target lib:
#   Make the Meta Library in all baselangs that support it.
lib: lib-python lib-perl lib-java lib-cpp

# target lib-%:
#   Create a baselang-specific version of the Meta Library
lib-%:
	@echo "##############################################################"
	metac -b $* -c root.meta

# target versions:
#   Show existing versions of the meta library
versions v:
	cd ../../lib/versions; ls -1 -F
	cd ../../lib; ls -l current previous

# target meta-%:
#   Create a new version of the Meta Library
meta-%:
	@if [ -d "../../lib/versions/meta-$*" ] ; then \
		echo "ERROR: ../../lib/versions/meta-$* already exists."; \
		exit 1; \
	fi		
	rm -rf .meta/oopl
	metac -b python -c root.meta parser2.meta lib.meta
	metac -b java -c root.meta lib.meta
	metac -b perl -c root.meta lib.meta
	cp -r .meta/oopl ../../lib/versions/meta-$*
	mkdir -p ../../lib/versions/meta-$*/meta
	cp *.meta ../../lib/versions/meta-$*/meta
	mkdir -p ../../lib/versions/meta-$*/meta/metaboot/auto
	cp *.py ../../lib/versions/meta-$*/meta/metaboot
	rm -rf ../../lib/versions/meta-$*/meta/metaboot/thirdparty
	cp -r thirdparty ../../lib/versions/meta-$*/meta/metaboot
	cp auto/*.py ../../lib/versions/meta-$*/meta/metaboot/auto
	rm -f ../../lib/previous
	if [ -e ../../lib/current ] ; then mv -f ../../lib/current ../../lib/previous; fi
	cd ../../lib; ln -s versions/meta-$* current
	cd ../../lib/versions; make meta-$*.tgz

# target unv:
#   Revert to the previous version
unv:
	@cd ../../lib; \
	if [ -d $$(readlink previous) ] ; then \
		echo "Removing $$(readlink current)"; \
		rm -rf $$(readlink current); \
		echo "Reinstating $$(readlink previous)"; \
		mv previous current; \
	else \
		echo "ERROR: No previous version found"; \
	fi
